﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using BBECServiceBusAdapter_CodeGenLib.AddForms.Revenue;

namespace BBECServiceBusAdapter.Helper_Activities.Inbound
{
    public sealed class ProcessPayment : CodeActivity
    {
        // Define an activity input argument of type string
        public InArgument<BBECServiceBusAdapter.ConstituentDataContractExtensions.PaymentDataContract> PaymentData { get; set; }
        public InArgument<Guid> ConstituentGuid { get; set; }


        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            //string text = context.GetValue(this.Data.).ToString;
            BBECServiceBusAdapter.ConstituentDataContractExtensions.PaymentDataContract inData = context.GetValue(this.PaymentData);
            Guid inConstitGuid = context.GetValue(this.ConstituentGuid);
            RevenueAddDataFormCustomData frmData = new RevenueAddDataFormCustomData();
            
            frmData.ConstituentID = inConstitGuid; // new Guid(inData.ConstituentID);
            frmData.Amount = inData.Amount;
            frmData.Description = inData.Description;
            frmData.Designation = inData.Designation;
            frmData.SubDesignation = inData.SubDesignation;
            frmData.ReceiptDate = inData.ReceiptDate;
            frmData.LookupID = inData.ID;
            frmData.PaymentMethod = inData.PaymentMethod;
            frmData.PostDate = inData.PostDate;
            frmData.RevenueDate = inData.RevenueDate;
            
            //Note that PledgeID is being passed in here as the LookupID. It will be translated in the SP
            foreach (AttributeData curAttr in inData.Attributes) {
                if (curAttr.AttributeName.ToUpper() == "PLEDGEID") {
                    //If it starts with WV_, don't try to process it
                    if (curAttr.AttributeValue.StartsWith("BBC_")) {
                        frmData.PledgeId= curAttr.AttributeValue.ToString();
                    }
                }
            }

            frmData.Save(new Blackbaud.AppFx.WebAPI.AppFxWebServiceProvider(Blackbaud.AppFx.Workflow.CatalogActivities.WebApiUtility.GetAppFxWebApiProvider(context).ConnectionString));
        }
    }
}
